#import "SMRootTabBarController.h"
#import "../Home/SMHomeViewController.h"
#import "../Circle/SMCircleViewController.h"
#import "../Trade/SMTradeViewController.h"
#import "../Mine/SMMineViewController.h"

@implementation SMRootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // ========== TabBar 外观（不透明+背景色+分割线） ==========
    self.tabBar.translucent = NO; // 关键：不透明
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *app = [[UITabBarAppearance alloc] init];
        [app configureWithOpaqueBackground];
        app.backgroundColor = [UIColor systemBackgroundColor];

        // 顶部分割线（shadow）
        app.shadowColor = [UIColor colorWithWhite:0 alpha:0.08];

        // 文字颜色
        NSDictionary *normalAttr = @{NSForegroundColorAttributeName: [UIColor secondaryLabelColor]};
        NSDictionary *selectedAttr = @{NSForegroundColorAttributeName: [UIColor systemBlueColor]};
        app.stackedLayoutAppearance.normal.titleTextAttributes = normalAttr;
        app.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttr;

        self.tabBar.standardAppearance = app;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = app;
        }
    } else {
        self.tabBar.barTintColor = [UIColor whiteColor];
        self.tabBar.tintColor = [UIColor systemBlueColor];
        self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
    }

    // ========== 子控制器 ==========
    UINavigationController *home  = [[UINavigationController alloc] initWithRootViewController:[SMHomeViewController new]];
    home.topViewController.title = @"首页";

    UINavigationController *circle = [[UINavigationController alloc] initWithRootViewController:[SMCircleViewController new]];
    circle.topViewController.title = @"圈子";

    UINavigationController *trade = [[UINavigationController alloc] initWithRootViewController:[SMTradeViewController new]];
    trade.topViewController.title = @"买卖";

    UINavigationController *mine  = [[UINavigationController alloc] initWithRootViewController:[SMMineViewController new]];
    mine.topViewController.title = @"我的";

    // ========== 先设置 tabBarItemsAttributes（数量与控制器一致） ==========
    // 注意：这些名字需要与 Assets 里的图片名称一致（见下面资源包）
    self.tabBarItemsAttributes = @[
        @{CYLTabBarItemTitle: @"首页", CYLTabBarItemImage: @"tab_home",  CYLTabBarItemSelectedImage: @"tab_home_sel"},
        @{CYLTabBarItemTitle: @"圈子", CYLTabBarItemImage: @"tab_circle",CYLTabBarItemSelectedImage: @"tab_circle_sel"},
        @{CYLTabBarItemTitle: @"买卖", CYLTabBarItemImage: @"tab_trade", CYLTabBarItemSelectedImage: @"tab_trade_sel"},
        @{CYLTabBarItemTitle: @"我的", CYLTabBarItemImage: @"tab_mine",  CYLTabBarItemSelectedImage: @"tab_mine_sel"}
    ];

    // ========== 再设置 viewControllers ==========
    self.viewControllers = @[home, circle, trade, mine];

    // ========== 兜底：如果资源图缺失，用系统符号占位，保证可运行 ==========
    for (UINavigationController *nav in self.viewControllers) {
        UITabBarItem *item = nav.tabBarItem;
        if (!item.image) {
            if (@available(iOS 13.0, *)) {
                NSString *title = item.title ?: @"";
                if ([title isEqualToString:@"首页"]) {
                    item.image = [UIImage systemImageNamed:@"house"];
                    item.selectedImage = [UIImage systemImageNamed:@"house.fill"];
                } else if ([title isEqualToString:@"圈子"]) {
                    item.image = [UIImage systemImageNamed:@"person.3"];
                    item.selectedImage = [UIImage systemImageNamed:@"person.3.fill"];
                } else if ([title isEqualToString:@"买卖"]) {
                    item.image = [UIImage systemImageNamed:@"cart"];
                    item.selectedImage = [UIImage systemImageNamed:@"cart.fill"];
                } else if ([title isEqualToString:@"我的"]) {
                    item.image = [UIImage systemImageNamed:@"person.circle"];
                    item.selectedImage = [UIImage systemImageNamed:@"person.circle.fill"];
                }
            }
        }
    }
}

@end
